use std::fmt;
use std::str::FromStr;

fn to_string_test() {
    struct Circle {
        radius: i32,
    }

    impl fmt::Display for Circle {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            write!(f, "Circle with radius: {}", self.radius)
        }
    }

    let c = Circle { radius: 10 };
    // the outputs of the following statements are the same
    println!("{}", c);
    println!("{}", c.to_string());
}

fn from_string_test() {
    #[derive(Debug)]
    struct Circle {
        radius: i32,
    }
    impl FromStr for Circle {
        type Err = ();

        fn from_str(s: &str) -> Result<Self, Self::Err> {
            let radius = s.parse::<i32>().map_err(|_| ())?;
            Ok(Circle { radius })
        }
    }
    let c = Circle::from_str("10").unwrap();
    println!("{:?}", c);
}

fn main() {
    to_string_test();
    from_string_test();
}